package chapter5.implement_disposablebean;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.GenericXmlApplicationContext;

public class DestructiveBeanWithInterface implements InitializingBean,
		DisposableBean {
	private String name;

	@Override
	public void destroy() throws Exception {
		System.out.println("Destroying Bean");
		if (name != null) {
			System.out.println("Clear name property null");
		}
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("Initializing Bean");
		if (name == null) {
			System.out.println("Set default name");
			name = "Chau Thang";
		} else {
			System.out.println("Name Properties :" + name);
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static void main(String[] args) {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:chapter5/implement_disposablebean/disposeInterface.xml");
		ctx.refresh();

		DestructiveBeanWithInterface bean = (DestructiveBeanWithInterface) ctx
				.getBean("destructiveBeanWithInterface");
		System.out.println("Bean begin detroy()");
		ctx.destroy();
		System.out.println("Bean detroy finish !");
	}
}
